# base_rule.py
from utils import date_handler,build_key

class BaseRule:
    def __init__(self, sheet_name, project_info):
        self.sheet_name = sheet_name
        self.project_info = project_info
        self.rules = {}

    def add_field(self, field_name, handler, **kwargs):
        key = build_key(self.sheet_name, field_name)
        self.rules[key] = {
            "handler": lambda p: handler({**p, 'project_info': self.project_info}),
            **kwargs
        }

    def get_rules(self):
        return self.rules
    
    def get_instance_value(self, key, alias=None, default="待补充"):
        value = self.project_info.get("instance_values", {}).get(key, None)
        if value is None and alias:
            value = self.project_info.get("instance_values", {}).get(alias, default)
        return value or default
if __name__ == '__main__':
    from utils import date_handler, build_key
    # rule = BaseRule('test', {})
    # rule.add_field('test', date_handler(1))
    # print(rule.get_rules())